<?php

namespace App\Exceptions;

use http\Exception;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Validation\ValidationException;
use Throwable;
use Illuminate\Database\QueryException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
use Illuminate\Support\Facades\Log;
use Illuminate\Database\Eloquent\ModelNotFoundException;

class Handler extends ExceptionHandler
{
    /**
     * A list of the exception types that are not reported.
     *
     * @var array
     */
    protected $dontReport = [
        //
    ];

    /**
     * A list of the inputs that are never flashed for validation exceptions.
     *
     * @var array
     */
    protected $dontFlash = [
        'current_password',
        'password',
        'password_confirmation',
    ];

    /**
     * Register the exception handling callbacks for the application.
     *
     * @return void
     */
    public function register()
    {
        $this->reportable(function (Throwable $e) {
            //
        });
    }

    public function report(Throwable $exception)
    {
        parent::report($exception);
    }

    public function render($request, Throwable $exception)
    {

        $code = 0;
        $msg = '';
        if ($exception instanceof QueryException) {
            $code = 1;
            $msg = $exception->getMessage();
        } else if ($exception instanceof ModelNotFoundException) {
            $code = 1;
            $msg = '模型未找到，请稍后重试';

        } else if ($exception instanceof \ReflectionException) {

            $code = 1;
            $msg = '网络错误,请联系客服！';
        } else if ($exception instanceof \ErrorException) {

            $code = 1;
            $msg = $exception->getMessage();
        } else if ($exception instanceof \Runtimeexception) {
            $code = 1;
            $msg = $exception->getMessage();
        } elseif ($exception instanceof ValidationException) {
            $code = 1;
            $msg = array_values($exception->errors())[0][0];
            return response()->json(['code' => $code, 'msg' => $msg, 'data' => '']);
        }
        if (!empty($msg) || !empty($exception->getMessage())) {
            Log::info('错误：', ['code' => $code ?? 1, 'msg' => $msg, 'data' => $exception->getMessage()]);
            return response()->json(['code' => $code, 'msg' => $msg, 'data' => '']);
        }
        /*if ($code > 0) {
            if (!empty($msg) || !empty($exception->getMessage())) {
                Log::info('错误：', ['code' => $code ?? 1, 'msg' => $msg, 'data' => $exception->getMessage()]);
                return response()->json(['code' => $code, 'msg' => $msg, 'data' => '']);
            }
        }else{
            if (!empty($msg) || !empty($exception->getMessage())) {
                Log::info('错误：', ['code' => 1, 'msg' => $msg, 'data' => $exception->getMessage()]);
                return response()->json(['code' => 1, 'msg' => $msg, 'data' => '']);
            }
        }*/

        return parent::render($request, $exception);
    }
}
